package com.sky.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> selectPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> dishVOList = dishMapper.selectPage(dishPageQueryDTO);
        PageInfo<DishVO> pageInfo = new PageInfo<>(dishVOList);
        PageResult pb = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pb);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @Override
    public Result insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());

        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.insert(dish);
        System.out.println(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));

            dishFlavorMapper.insert(flavors);
            cleanCache("dish_"+dish.getCategoryId());
        }
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public Result<DishVO> selectById(Long id) {
        DishVO dishVO = dishMapper.selectById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @Override
    public Result update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dish.getId());
        dishDTO.getFlavors().forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
        dishFlavorMapper.insert(dishDTO.getFlavors());
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setStatus(status);
        dishMapper.updateStatus(dish);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<Long> ids) {
        Integer status = null;
        for (Long id : ids) {
            DishVO dishVO = dishMapper.selectById(id);
             status = dishVO.getStatus();

        }
        if(status == 1){
                return Result.error("菜品正在售卖中，无法删除");
        }else {
            dishFlavorMapper.deleteByDishIds(ids);
            dishMapper.deleteByIds(ids);
            cleanCache("dish_*");
            return Result.success();
        }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public Result<List<Dish>> selectByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.selectByCategoryId(categoryId);
        return Result.success(dishList);
    }

    /**
     * 清理缓存
     * @param keyPattern
     */
    public void cleanCache(String keyPattern){
        Set keys = redisTemplate.keys(keyPattern);
        keys.forEach(key -> redisTemplate.delete(key));
    }
}
